package cn.afactory.tygz.ui.base.utils;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Kenneth on 15/9/10.
 */
public class StringUtils {
    /**
     * 多种组合颜色的字体转换
     *
     * @param strs
     * @param color
     * @return
     */
    public static Spannable convertSpannableString(String[] strs, int[] color) {
        if (strs == null || color == null) {
            return null;
        } else if (strs.length != color.length) {
            throw new RuntimeException(
                    "The strs length must equals the color's.");
        } else {
            SpannableStringBuilder builder = new SpannableStringBuilder();
            for (int i = 0; i < strs.length; i++) {
                String str = strs[i];
                SpannableString ss = new SpannableString(str);
                ss.setSpan(new ForegroundColorSpan(color[i]), 0, str.length(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                builder.append(ss);
            }
            return builder;
        }
    }

    public static String textFormatEllipsis(String text, int maxLength) {
        if (!TextUtils.isEmpty(text)) {
            if (text.length() > maxLength) {
                text = text.substring(0, maxLength) + "...";
            }
        }
        return text;
    }

    public static Spannable replaceAll(String orgString, String repString, int color) {
        SpannableString ss = new SpannableString(orgString);
        int pos = orgString.indexOf(repString);
        while (pos > -1) {
            ss.setSpan(new ForegroundColorSpan(color), pos, pos + repString.length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            pos = orgString.indexOf(repString, pos + repString.length());
        }

        return ss;
    }

    public static Spannable replaceAll(String orgString, List<String> repString, int color) {
        SpannableString ss = new SpannableString(orgString);
        if (repString != null && !repString.isEmpty()) {
            for (String item : repString) {
                int pos = orgString.indexOf(item);
                while (pos > -1 && item.length() > 0) {
                    ss.setSpan(new ForegroundColorSpan(color), pos, pos + item.length(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    pos = orgString.indexOf(item, pos + item.length());
                }
            }
        }


        return ss;
    }

    public static String formartDistance(double distance) {
        if (distance > 1000) {
            return new DecimalFormat("###.00").format(distance / 1000).toString() +"km";
        } else if (distance <= 1000 && distance > 0) {
            return new DecimalFormat("###").format(distance).toString() +"m";
        } else {
            return "";
        }
    }

    public static String nullToEmpty(String str) {
        if (null == str) {
            return "";
        }

        return str;
    }

    public static boolean isEmpty(String str) {
        if (null == str) {
            return true;
        }

        if (str.trim().length() == 0) {
            return true;
        }

        return false;
    }
}
